package org.dsbackend.exception;


import org.dsbackend.entity.JsonData;
import org.dsbackend.entity.ResultException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        JsonData r = new JsonData(0,"",null);
        if (e instanceof ResultException) {
            r.setErr(((ResultException) e).getCode());
        }
        else{
            r.setErr(500);
        }
        r.setMsg(e.getMessage());
        return r;
    }
}
